package entity

import (
	"encoding/json"
	"time"
)

type Day7 struct {
	RoleID    int32  `gorm:"type:int;primary_key"`
	Day       int32  // 天数
	Record    []byte `gorm:"type:jsonb"` // json.Marshal(self.record)
	CreatedAt time.Time
	UpdatedAt time.Time

	recordMap map[int32]bool `sql:"-"`
}

func (e *Day7) AfterFind() error {

	e.recordMap = make(map[int32]bool)

	_ = json.Unmarshal(e.Record, &e.recordMap)
	return nil
}

func (e *Day7) BeforeSave() error {

	if e.recordMap == nil {
		e.recordMap = make(map[int32]bool)
	}

	e.Record, _ = json.Marshal(e.recordMap)
	return nil
}

func (e *Day7) BeforeUpdate() error {

	if e.recordMap == nil {
		e.recordMap = make(map[int32]bool)
	}

	e.Record, _ = json.Marshal(e.recordMap)
	return nil
}

func (e *Day7) GetRecord(day int32) bool {
	return e.recordMap[day]
}

func (e *Day7) SetRecord(day int32) {
	e.recordMap[day] = true
}
